package com.java1412.web.controller.api;

import com.java1412.common.dto.UserDto;
import com.java1412.common.entity.User;
import com.java1412.common.utils.ConstantUtil;
import com.java1412.common.utils.R;
import com.java1412.common.utils.ResponseEnum;
import com.java1412.web.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author java1412
 * @since 2025-05-16 14:03
 */
@RestController
@RequestMapping(value = "/api/user")
@Api(tags = "前台用户接口")
public class ApiUserController {

    @Resource
    private UserService userService;

    @GetMapping(value = "/{id}")
    public R findById(@PathVariable(value = "id") Integer id) {
        return userService.selectById(id);
    }

    @PostMapping(value = "/login")
    public R login(UserDto userDto, HttpSession session) {
//        System.out.println(userDto);
//        System.out.println(userDto.getPhone() + "," + userDto.getLoginPassword());
//        return R.r(ResponseEnum.USER_LOGIN_SUCCESS);
        R r = userService.apiLogin(userDto);
        // 登录成功需要绑定session
        if (Integer.parseInt(r.get("code").toString()) == 1000) {
            // 登录成功
            session.setAttribute(ConstantUtil.LOGIN_USER_KEY, r.get("data"));
        }
        return r;
    }

    // 获取登录的用户信息
    @GetMapping(value = "/getCurrentUser")
    @ApiOperation(value = "获取当前用户信息")
    public R getCurrentUser(HttpSession session) {
        Object loginUser = session.getAttribute(ConstantUtil.LOGIN_USER_KEY);
        return loginUser == null ? R.r(ResponseEnum.USER_NOT_LOGIN) : R.r(ResponseEnum.USER_LOGINNED, loginUser);
    }

    // 注销
    @GetMapping(value = "/logout")
    @ApiOperation(value = "获取当前用户信息")
    public R logout(HttpSession session) {
        R r = null;
        try {
            session.removeAttribute(ConstantUtil.LOGIN_USER_KEY);
            r = R.r(ResponseEnum.USER_LOGOUT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            r = R.r(ResponseEnum.USER_LOGOUT_FAIL);
        }
        return r;
    }

    // 注销
    @PostMapping(value = "/register")
    @ApiOperation(value = "注册新用户")
    public R register(UserDto userDto) {
        R r = userService.add(userDto);
        return r;
    }

    // 实名认证
    @PostMapping(value = "/rna")
    @ApiOperation(value = "实名认证")
    public R realNameAuthentication(UserDto userDto, HttpSession session) {
        System.out.println(userDto);
        R r = userService.realNameAuthentication(userDto);
        if (Integer.parseInt(r.get("code").toString()) == 1017) {
            // 实名认证成功
            User loginUser = (User) session.getAttribute(ConstantUtil.LOGIN_USER_KEY);
            R r1 = userService.selectById(loginUser.getId());
            session.setAttribute(ConstantUtil.LOGIN_USER_KEY, r1.get("data"));
        }
        return r;
    }
}
